package app.controller.sys;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import app.model.sys.SysMenu;
import app.service.sys.SysMenuService;
import base.JsonResult;
import base.PageInfo;
import base.tree.MenuTree;
import base.tree.MenuTreeChecked;

@Controller
@RequestMapping("/sys/menu")
public class SysMenuController {
	
	private Logger logger = Logger.getLogger(SysMenuController.class);
	
	@Resource
	private SysMenuService sysMenuService;
	
	@ResponseBody
	@RequestMapping("/tree.do")
	public MenuTree tree(){
		List<Map<String, Object>> queryMenuList = sysMenuService.queryMenuList(false);
		MenuTree menuTree = new MenuTree();
		menuTree.buildTree(queryMenuList,"menuName","parentId");
		return menuTree;
	}
	
	@ResponseBody
	@RequestMapping("/treeFull.do")
	public MenuTree treeFull(){
		List<Map<String, Object>> queryMenuList = sysMenuService.queryMenuList(true);
		MenuTree menuTree = new MenuTree();
		menuTree.buildTree(queryMenuList,"menuName","parentId");
		return menuTree;
	}
	
	@ResponseBody
	@RequestMapping("/treeChecked.do")
	public MenuTreeChecked treeCheck(){
		List<Map<String, Object>> queryMenuList = sysMenuService.queryMenuList(false);
		MenuTreeChecked menuTreeChecked = new MenuTreeChecked();
		menuTreeChecked.buildTree(queryMenuList,"menuName","parentId");
		return menuTreeChecked;
	}
	
	@ResponseBody
	@RequestMapping("/queryList.do")
	public PageInfo queryList(PageInfo pageInfo){
		return sysMenuService.queryPageList(pageInfo);
	}
	
	@ResponseBody
	@RequestMapping("/save.do")
	public JsonResult save(SysMenu sysMenu){
		if(StringUtils.isEmpty(sysMenu.getParentId())){
			sysMenu.setParentId(MenuTree.root);
		}
		if(StringUtils.isEmpty(sysMenu.getId())){
			sysMenuService.saveEntity(sysMenu);
		}else{
			sysMenuService.updateEntity(sysMenu);
		}
		return new JsonResult("操作成功");
	}
	
	@ResponseBody
	@RequestMapping("/view.do")
	public JsonResult view(String id){
		SysMenu sysMenu = sysMenuService.queryEntityById(id);
		return new JsonResult("操作成功",sysMenu);
	}
	
	@ResponseBody
	@RequestMapping("/enable.do")
	public JsonResult enable(String id){
		sysMenuService.enable(id);
		return new JsonResult("操作成功");
	}
	
	@ResponseBody
	@RequestMapping("/disable.do")
	public JsonResult disable(String id){
		sysMenuService.disable(id);
		return new JsonResult("操作成功");
	}
	
	@ResponseBody
	@RequestMapping("/del.do")
	public JsonResult del(String ids){
		sysMenuService.delEntity(ids);
		return new JsonResult("操作成功");
	}
}
